﻿using System;
using System.Linq;
using ClusterAware.Properties;
using System.Management;
using ClusterAwareProperties;
using ClusterAwareWMI;

namespace ClusterAware
{
    /// <summary>
    /// Wrapper property to Network Class actions
    /// </summary>
    public class NetworkMethods
    {

        #region private properties

        private readonly ClusterManager _managerclass;

        #endregion

        #region Ctor

        /// <summary>
        /// private Constructor
        /// </summary>
        private NetworkMethods()
        {
        }

        /// <summary>
        /// Create Class NetworkMethods
        /// </summary>
        /// <param name="managerclass">Instance of ClusterManager class</param>
        public NetworkMethods(ClusterManager managerclass)
            : this()
        {
            _managerclass = managerclass;
        }

        #endregion

        #region public Methods

        /// <summary>
        /// CusterManager Instance
        /// </summary>
        public ClusterManager ManagerInstance
        {
            get { return _managerclass; }
        }
        
        
        /// <summary>
        /// if Exists Network
        /// </summary>
        /// <param name="networkName">Network name</param>
        /// <returns>True if exists</returns>
        ///<exception cref="ClusterManagerException">
        /// this method throws exception for the following conditions:
        /// Not Connected, 
        /// Parameter Network name is empty
        /// </exception>
        public bool ExistNetwork(string networkName)
        {
            if (!_managerclass.IsConnected)
            {
                throw new ClusterManagerException(Resources.Error_NotConnected);
            }
            if (string.IsNullOrEmpty(networkName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamEmpty, "networkName"));
            }
            if (_managerclass.CacheInstances)
            {
                return _managerclass.NetworkInstances.ContainsKey(networkName);
            }
            try
            {
                var net = new ClusterWmiHelpper().GetNetwork(networkName, _managerclass.ScopeManager);
                return (net != null);
            }
            catch
            {
                return false;
            }
        }


        /// <summary>
        /// get list of Netowrk
        /// </summary>
        /// <returns>Array of class Network</returns>
        ///<exception cref="ClusterManagerException">
        /// this method throws exception for the following conditions:
        /// Not Connected
        /// </exception>
        public NetworkProperty[] GetNetworks()
        {
            if (!_managerclass.IsConnected)
            {
                throw new ClusterManagerException(Resources.Error_NotConnected);
            }
            try
            {
                return _managerclass.CacheInstances ? _managerclass.NetworkInstances.Values.ToArray() : new ClusterWmiHelpper().GetNetworks(_managerclass.ScopeManager);
            }
            catch (ManagementException ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Networks.GetNetworks", ex.Message), ex);
            }
            catch (Exception ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Networks.GetNetworks", ex.Message), ex);
            }
        }
        /// <summary>
        /// Get a Network
        /// </summary>
        /// <param name="networkName">name Network</param>
        /// <returns>Network class</returns>
        ///<exception cref="ClusterManagerException">
        /// this method throws exception for the following conditions:
        /// Not Connected, 
        /// Parameter Network name is empty, 
        /// Network not exists
        /// </exception>
        public NetworkProperty GetNetwork(string networkName)
        {
            if (!_managerclass.IsConnected)
            {
                throw new ClusterManagerException(Resources.Error_NotConnected);
            }
            if (string.IsNullOrEmpty(networkName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamEmpty, "networkName"));
            }
            if (_managerclass.CacheInstances)
            {
                if (!_managerclass.NetworkInstances.ContainsKey(networkName))
                {
                    throw new ClusterManagerException(string.Format(Resources.Error_ParamNotExist, "Network", networkName));
                }
                return _managerclass.NetworkInstances[networkName];
            }
            try
            {
                return new ClusterWmiHelpper().GetNetwork(networkName, _managerclass.ScopeManager);
            }
            catch (ManagementException ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Networks.GetNetwork", ex.Message), ex);
            }
            catch (Exception ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Networks.GetNetwork", ex.Message), ex);
            }
        }

        ///<summary>
        /// Save a Network in Cluster
        ///</summary>
        ///<param name="networkClass">Network CLass to Save</param>
        /// <returns>
        /// NetworkProperty class
        /// </returns>
        public NetworkProperty Save(NetworkProperty networkClass)
        {
            if (!_managerclass.IsConnected)
            {
                throw new ClusterManagerException(Resources.Error_NotConnected);
            }
            if (networkClass == null)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamEmpty, "Networkclass"));
            }
            if (_managerclass.CacheInstances)
            {
                if (!_managerclass.NetworkInstances.ContainsKey(networkClass.Name))
                {
                    throw new ClusterManagerException(string.Format(Resources.Error_ParamNotExist, "Network", networkClass.Name));
                }
            }
            else
            {
                if (!ExistNetwork(networkClass.Name))
                {
                    throw new ClusterManagerException(string.Format(Resources.Error_ParamNotExist, "Network", networkClass.Name));
                }
            }
            try
            {
                networkClass.SaveManagementObject(_managerclass.OperationSystem);
                var ret = new ClusterWmiHelpper().GetNetwork(networkClass.Name, _managerclass.ScopeManager);
                if (_managerclass.CacheInstances)
                {
                    _managerclass.NetworkInstances[networkClass.Name] = ret;
                }
                return ret;
            }
             catch (ManagementException ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Networks.Save", ex.Message), ex);
            }
            catch (Exception ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Networks.Save", ex.Message), ex);
            }
        }

        #endregion
    }
}
